//
//  OAFinancialTable2VC.m
//  OA项目
//
//  Created by innofive on 2018/5/2.
//  Copyright © 2018年 inno. All rights reserved.
//

#import "OAFinancialTable2VC.h"
#import "OAFinancialTable3Cell.h"
@interface OAFinancialTable2VC ()<RATreeViewDataSource,RATreeViewDelegate>
@property (nonatomic,strong) RATreeView *raTreeView;
@property (nonatomic,strong) NSMutableArray *modelArray;//存储model的数组
@end

@implementation OAFinancialTable2VC

- (void)viewDidLoad {
    
    
    [super viewDidLoad];

    //5     宝鸡市 (四层)
    RaTreeModel *zijingcun = [RaTreeModel dataObjectWithName:@"紫荆村" children:nil];
    //4
    RaTreeModel *chengcunzheng = [RaTreeModel dataObjectWithName:@"陈村镇" children:@[zijingcun]];
    
    //3
    RaTreeModel *fengxiang = [RaTreeModel dataObjectWithName:@"凤翔县" children:@[chengcunzheng]];
    RaTreeModel *qishan = [RaTreeModel dataObjectWithName:@"岐山县" children:nil];
    
    //2
    RaTreeModel *baoji = [RaTreeModel dataObjectWithName:@"宝鸡市" children:@[fengxiang,qishan]];
    
    
    
    //3     西安市
    RaTreeModel *yantaqu = [RaTreeModel dataObjectWithName:@"雁塔区" children:nil];
    RaTreeModel *xinchengqu = [RaTreeModel dataObjectWithName:@"新城区" children:nil];
    
    //2
    RaTreeModel *xian = [RaTreeModel dataObjectWithName:@"西安" children:@[yantaqu,xinchengqu]];
    
    //1
    RaTreeModel *shanxi = [RaTreeModel dataObjectWithName:@"一、销售费用" children:@[baoji,xian]];
    RaTreeModel *shanxi1 = [RaTreeModel dataObjectWithName:@"二、销售费用" children:@[baoji,xian]];
    RaTreeModel *shanxi2 = [RaTreeModel dataObjectWithName:@"三、财务费用" children:@[baoji,xian]];
    
    [self.modelArray addObject:shanxi];
    [self.modelArray addObject:shanxi1];
    [self.modelArray addObject:shanxi2];

    
    
}

-(void)onLoadViewByWillAppear:(UIView *)view
{
    UIButton *btn = ({
        UIButton *l = [UIButton buttonWithType:UIButtonTypeCustom];
        l.backgroundColor = [UIColor whiteColor];
        [l setNormalTitleColor:[UIColor blackColor]];
        l.titleLabel.font =kSysFont(kFitW(12));
        [l setSelectTitleColor:hexCOLOR(@"B8944D")];
        [l setNormalTitle:@"2017年1-6月合计"];
        [l setNormalImage:IMAGE(@"iconSlideDown")];
        [view addV:l];
        l;
    });
    UIButton *btn1 = ({
        UIButton *l = [UIButton buttonWithType:UIButtonTypeCustom];
        l.titleLabel.font =kSysFont(kFitW(12));
        [l setNormalTitleColor:[UIColor blackColor]];
        [l setSelectTitleColor:hexCOLOR(@"B8944D")];
        [l setNormalTitle:@"经营班子"];
        l.backgroundColor = [UIColor whiteColor];
        [l setNormalImage:IMAGE(@"iconSlideDown")];
        [view addV:l];
        l;
    });
    
    UILabel *label = ({
        UILabel *l = [UILabel new];
        l.font = kSysFont(kFitW(16));
        l.text =@"项目";
        l.textAlignment = NSTextAlignmentLeft;
        l.textColor = [UIColor blackColor];
//        l.backgroundColor = [UIColor redColor];
        [view addV:l];
        l;
    });
    
    UILabel *label1 = ({
        UILabel *l = [UILabel new];
        l.font = kSysFont(kFitW(16));
        l.text =@"金额";
        l.textColor = [UIColor blackColor];
        l.textAlignment = NSTextAlignmentRight;
//        l.backgroundColor = [UIColor redColor];
        [view addV:l];
        l;
    });


    RATreeView *raTreeView = ({
        RATreeView *l = [[RATreeView alloc]initWithFrame:view.frame];
        l.delegate = self;
        l.dataSource = self;
        [view addV:l];
        l;
    });
    _raTreeView = raTreeView;
    
    UIView *l = [UIView new];
    l.backgroundColor = [UIColor colorWithHexString:@"#dbdbdb"];
    [view addSubview:l];
    
    [self onMainQueueAddSub:^(UIView *view, CGPoint origin, CGSize size, CGPoint center) {
        CGFloat w = size.width/2;
        
        btn.frame = CGRectMake(0, 0,w, kFitH(50));
        [btn layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleRight) imageTitleSpace:3];
        
        btn1.frame = CGRectMake(w, 0,w, kFitH(50));
        [btn1 layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleRight) imageTitleSpace:3];
        
        
        [label sizeToFit];
        label.left =kFitW(20);
        label.top =kFitH(50);
        
        [label1 sizeToFit];
        label1.right =size.width-kFitW(20);
        label1.top =kFitH(50);
        
        raTreeView.frame = CGRectMake(0,label1.bottom+kFitH(20), size.width,size.height-label1.bottom-kFitH(20));
        
        l.frame = CGRectMake(0, label1.bottom+kFitH(20), size.width, kFitW(0.5));
    }];
}


//返回行高
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item {
    
    return kFitH(60);
}

//将要展开
- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item {
    
//    OAFinancialTable3Cell *cell = (OAFinancialTable3Cell *)[treeView cellForItem:item];
    //cell.iconView.image = [UIImage imageNamed:@"open"];
    
}
//将要收缩
- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item {
    
//    OAFinancialTable3Cell *cell = (OAFinancialTable3Cell *)[treeView cellForItem:item];
    //    cell.iconView.image = [UIImage imageNamed:@"close"];
    
}

//已经展开
- (void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item {
    
    NSLog(@"已经展开了");
}
//已经收缩
- (void)treeView:(RATreeView *)treeView didCollapseRowForItem:(id)item {
    
    NSLog(@"已经收缩了");
}


#pragma mark -----------dataSource

//返回cell
- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item {
    

    //获取cell
    OAFinancialTable3Cell *cell = [treeView dequeueReusableCellWithIdentifier:@"RaTreeViewCell"];
    if (!cell) {
        cell = [[OAFinancialTable3Cell alloc]initWithStyle:(UITableViewCellStyleDefault) forViewController:self reuseIdentifier:@"RaTreeViewCell" forItem:item];
    }
    //当前item
    RaTreeModel *model = item;
    //当前层级
    NSInteger level = [treeView levelForCellForItem:item];
    model.level = level;
    //赋值
    [cell update:model forRATreeView:treeView AtItem:item];
    
    return cell;
    
}

/**
 *  必须实现
 *
 *  @param treeView treeView
 *  @param item    节点对应的item
 *
 *  @return  每一节点对应的个数
 */
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    RaTreeModel *model = item;
    
    if (item == nil) {
        
        return self.modelArray.count;
    }
    
    return model.children.count;
}
/**
 *必须实现的dataSource方法
 *
 *  @param treeView treeView
 *  @param index    子节点的索引
 *  @param item     子节点索引对应的item
 *
 *  @return 返回 节点对应的item
 */
- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item {
    
    RaTreeModel *model = item;
    if (item==nil) {
        
        return self.modelArray[index];
    }
    
    return model.children[index];
}


//cell的点击方法
- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item {
    
    //获取当前的层
    NSInteger level = [treeView levelForCellForItem:item];
    
    //当前点击的model
    RaTreeModel *model = item;
    
    NSLog(@"点击的是第%ld层,name=%@",level,model.name);
    
}

//单元格是否可以编辑 默认是YES
- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item {
    
    return NO;
}
//懒加载
- (NSMutableArray *)modelArray {
    if (!_modelArray) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
